<?php

require_once(PRESSLESS_PRODUCT_DIR . '/libs/negotiation-functions.php');

/**
 * A Pressless_Action_Handler handles requests for a given resource class.
 */
class Pressless_Action_Handler
{
  private $format_support = array();
  private $language_support = array();

  public function __construct()
  {
  }

  private function calc_preferred_format()
  {
    $accept = http_parse_accept_header( $_SERVER['HTTP_ACCEPT'] );
  }

  private function calc_preferred_language()
  {
    $accept_langs = http_parse_accept_header( $_SERVER['HTTP_ACCEPT'] );
  }

  public function handle_request()
  {
    $this->calc_preferred_format();

    $request_method  = strtolower($_SERVER['REQUEST_METHOD']);

    // Call the correct request handler method depending on the request method
    if ( method_exists($this, $request_method) )
    {
      call_user_func(array($this, $request_method));
    }
    else
    {
      header("HTTP/1.1 405: Method Not Supported");
    }
  }

  /**
   * http://blog.taragana.com/index.php/archive/how-to-respond-to-http-head-request-from-php/
   */
  protected function head()
  {
    return $this->get();
  }

  protected function get()
  {
    header("HTTP/1.1 405: Method Not Supported");
  }

  protected function apply_template($filename, $vars = array(), $include_globals = true)
  {
    global $pressless_action_files;

    if ( $filename[0] != '/' ) // Is it not an absolute path?
    {
      if ( strpos($filename, '/') === FALSE )
        $filename = PRESSLESS_ACTION . '/' . $filename;

      $filename = $pressless_action_files[$filename];
    }

    return apply_template($filename, &$vars, $include_globals);
  }
}

?>
